package cn.com.user.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.cabinet.web.entity.Diauthority;

import cn.com.cabinet.dao.DiauthorityDao;
import cn.com.cabinet.dto.UserDto;
import cn.com.cabinet.service.UserServiceImpl;
import cn.com.easy.utils.CookieUtils;

@Controller
@RequestMapping("/user")
public class UserController {
	@Autowired
	private UserServiceImpl userService;
	
	@Autowired
	private DiauthorityDao diauthorityDao;
	
	@RequestMapping()
	public String user(ModelMap modelMap,HttpServletRequest request,HttpServletResponse response) throws Exception{
		String userid = CookieUtils.getCookieValue(request, "userid");
		if(userid == "null" || userid == null){
			//System.out.println(request.getServletPath());
			CookieUtils.setCookieRedirectUrl(request, response, request.getServletPath());
			return "redirect:/login";
		}else{
			//获取用户信息
			UserDto udto= userService.getUser(Long.valueOf(userid));
			modelMap.addAttribute("udto", udto);
			//System.out.println(udto);
			return "/user/user";
		}	
	}

	@RequestMapping("/detail")
	public String userDetail(ModelMap modelMap,HttpServletRequest request,HttpServletResponse response) throws Exception{
		String userid = CookieUtils.getCookieValue(request, "userid");
		if(userid == "null" || userid == null){
			//System.out.println(request.getServletPath());
			CookieUtils.setCookieRedirectUrl(request, response, request.getServletPath());
			return "redirect:/login";
		}else{
			//获取用户信息
			UserDto udto= userService.getUser(Long.valueOf(userid));
			modelMap.addAttribute("udto", udto);
			
			Diauthority d = diauthorityDao.getDauthorityByUserId(udto.getId());
			if(d != null){
				modelMap.addAttribute("li", 1);
			}
			
			//System.out.println(udto);
			return "/user/user_detail";
		}	
		
		
	}
	
	@RequestMapping("/order")
	public String userOder(){
		return "/user/user_order";
		
	}
}
